﻿using MediatR;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.BinLocationCommands;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.BinLocationCommandsHandlder
{
    public class BinLocationGetWarehouseListCommandHandlder : IRequestHandler<BinLocationGetWarehouseListCommand, ApiResult<List<WarehouseDTO>>>
    {
        private readonly ILogger<BinLocationGetWarehouseListCommandHandlder> _logger;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public BinLocationGetWarehouseListCommandHandlder(ILogger<BinLocationGetWarehouseListCommandHandlder> logger, IWarehouseInfoRepository warehouseInfoRepository)
        {
            _logger = logger;
            _warehouseInfoRepository = warehouseInfoRepository;
        }
        public async Task<ApiResult<List<WarehouseDTO>>> Handle(BinLocationGetWarehouseListCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<WarehouseDTO>> result = new ApiResult<List<WarehouseDTO>>();
            try
            {
                _logger.LogInformation("显示仓库类型成功");
                var list = from a in await _warehouseInfoRepository.GetAllAsync()
                           where a.IsDelete == false
                           select new WarehouseDTO
                           {
                               WarehouseId = a.WarehouseId,
                               WarehouseName = a.WarehouseName,
                           };

                result.Data = list.ToList();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
